package top.dprkshow.springboothelloworld.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HAN Bing
 * @date 2021/6/25
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("top.dprkshow.springboothelloworld.api"))
                .paths(PathSelectors.any())
                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts())
                ;
    }

    private ApiInfo apiInfo() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return new ApiInfoBuilder()
                .title("HelloWorld测试（配置文件title）")
                .description("测试（配置文件description）")
                .contact(new Contact("Kent HAN","http://dprkshow.com","kkk@hhh.com"))
                .version("1.0")
                .build();
    }

//    private List<SecurityScheme> securitySchemes() {
//        List list = new ArrayList<SecurityScheme>();
//        list.add(new ApiKey("Authorization", "Authorization", "header"));
//        return list;
//    }

//    private List<SecurityContext> securityContexts() {
//        List list = new ArrayList<SecurityContext>();
//        list.add( SecurityContext.builder()
//                .securityReferences(defaultAuth())
//                .forPaths(PathSelectors.regex("^(?!auth).*$"))
//                .build());
//        return list;
//    }

//    List<SecurityReference> defaultAuth() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        List<SecurityReference> list=new ArrayList();
//        list.add(new SecurityReference("Authorization", authorizationScopes));
//        return list;
//    }
}
